package com.ardf.service.impl;

import com.ardf.entity.Card;
import com.ardf.entity.Print;
import com.ardf.entity.ReplaceCard;
import com.ardf.entity.dto.*;
import com.ardf.mapper.CardMapper;
import com.ardf.service.ICardService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@Service
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements ICardService {
    private final CardMapper cardMapper;

    public CardServiceImpl(CardMapper cardMapper) {
        this.cardMapper = cardMapper;
    }

    @Override
    public List<UnitMenuDto> getMenu(Long competitionId) {
        return cardMapper.getMenu(competitionId);
    }

    @Override
    public Page<CardSearchAllDto> searchAll(Integer pageSize, Integer pageNum, Long unitId, String searchAll) {
        Page<NumberSearchAllDto> page = new Page<>(pageNum,pageSize);
        return cardMapper.searchAll(page, unitId, searchAll);
    }

    @Override
    public Boolean undateTPCard(Card card) {
        int result = cardMapper.update(new LambdaUpdateWrapper<Card>()
                .eq(Card::getId,card.getId())
                .set(Card::getNumber,card.getNumber())
        );
        if(result==1){
            return true;
        }else{
            return false;
        }
    }


    @Override
    public Boolean insertTPCard(CardDto card) {
        int result = cardMapper.insertReplaceCard(card);
        if(result==1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * author:容伟洪
     * time:2025-2-24
     * description：根据赛事id查询指卡号是否在当前赛事中存在
     */
    @Override
    public Boolean selectByComAndNum(Long competitionId,String number) {
        List<CardDto> cards = cardMapper.selectCardByComId(competitionId,number);
        List<CardDto> replaceCards = cardMapper.selectReplaceCardAll(competitionId,number);
        cards.addAll(replaceCards);
        if(cards.size()>0){
            return false;
        }
        return true;
    }



    public Card selectByTp(String number,Long projectId){
        Card card=cardMapper.selectAthleteIdByNumAndPId(number,projectId);
        return card;
    }


    @Override
    public Page<ReplaceCardTableDto> replaceCardSearchAll(Integer pageSize, Integer pageNum, Long competitionId, String searchAll) {
        Page<NumberSearchAllDto> page = new Page<>(pageNum,pageSize);
        return cardMapper.replaceCardSearchAll(page, competitionId, searchAll);
    }

    @Override
    public List<CardDto> selectReplaceCardByAthId(Long athleteId) {
        return cardMapper.selectReplaceCardByAthId(athleteId);
    }

    @Override
    public ReplaceCard selectReplaceCardByComIdAndNum(Long competitionId, String number) {
        return cardMapper.selectReplaceCardByComId(competitionId,number);
    }

    @Override
    public Print selectPrintByAthId(Long athId) {
        return cardMapper.selectPrintByAthId(athId);
    }

    @Override
    public Print selectPrintById(Long id) {
        return cardMapper.selectPrintById(id);
    }


}
